﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;
using ThreeByte.ActiveDeck.Service;
using ThreeByte.ActiveDeck.Data.UserResponse;
using System.IO;
using ThreeByte.Media;
using System.Windows.Threading;
using System.Windows;
using ThreeByte.ActiveDeck.Config;
using Excel = Microsoft.Office.Interop.Excel;
using log4net;

namespace ThreeByte.ActiveDeck.AddIn.Content.UserResponse
{
    public class CategoriesUpdater : UserResponseFeedbackUpdater
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(CategoriesUpdater));


        private CategoriesConfig QuestionConfig;
        private CategoriesUserData UserData;

        private Dispatcher Dispatcher;

        public CategoriesUpdater(string presentation, PowerPoint.Shape shape, WebServiceClient client, CategoriesConfig questionConfig, Dispatcher dispatcher)
            : base(presentation, shape, client) {

            QuestionConfig = questionConfig;
            UserData = questionConfig.InitUserData();
            Dispatcher = dispatcher;
        }

        protected override bool UpdateUserData(System.Xml.Linq.XElement userData) {
            AggregateUserData newUserData = AggregateUserData.ParseXml(userData);
            return UserData.UpdateUserData(newUserData);
        }

        protected override void UpdateGraphic() {

            //Marshal to the dispatcher thread
            Dispatcher.BeginInvoke((Action)delegate() {
                log.Debug("Updating categories Graphic");

                try {
                    //Create this on the main thread
                    log.Debug("Construct new graphic");
                    CategoriesDataUserControl2 graphicToRender = new CategoriesDataUserControl2();
                    log.Debug("Set Categories Config");
                    graphicToRender.Config = QuestionConfig;

                    log.Debug("Set Refresh Graphic");
                    graphicToRender.Refresh(UserData);

                    string tempFilename = System.IO.Path.GetTempFileName();

                    Size renderSize = new Size(1280, 720);
                    //log.Debug("Set custom graphic size");
                    //Size renderSize = new Size(MyShape.Width, MyShape.Height);
                    ImageUtil.SaveToBitmap(graphicToRender, tempFilename, renderSize);
                    log.Debug("Bitmap Saved");

                    PowerPoint.Slide thisSlide = (PowerPoint.Slide)(MyShape.Parent);

                    PowerPoint.Shape newShape = thisSlide.Shapes.AddPicture(tempFilename, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoTrue,
                        MyShape.Left, MyShape.Top, MyShape.Width, MyShape.Height);


                    //Store Section/Page info to the shape
                    newShape.Name = @"ActiveDeck_Categories";

                    newShape.Tags.Add("Section", MyShape.Tags["Section"]);
                    newShape.Tags.Add("Page", MyShape.Tags["Page"]); //TODO: Get the page business figured out
                    newShape.Tags.Add("ConfigType", MyShape.Tags["ConfigType"]);

                    MyShape.Delete();
                    MyShape = newShape;
                } catch(Exception ex) {
                    log.Error("Error updating Categories graphic", ex);
                }
            }, null);
        }

    }
}
